<template>
	<view>

	</view>
</template>
<script lang="uts">
	import { SVGAPlayer, SVGAParser, SVGAVideoEntity, SVGAPlayerDelegate } from "SVGAPlayerUTS"
	import { NSError, URL, Float } from 'Foundation'

	export default {
		name: "svga-player",
		emits: ['start', 'error', 'finished', 'frame', 'percentage'],
		props: {
			/**
			* 动画资源地址，支持绝对路径和http、https地址
			*/
			"path": {
				type: String,
				default: ""
			},
			/**
			 * 动画是否循环播放
			 */
			"loop": {
				type: Boolean,
				default: false
			},
			/**
			 * Backward  Forward  Clear
			 */
			"fillMode": {
				type: String,
				default: "Backward"
			},
			/**
			 * 是否隐藏动画
			 */
			"hidden": {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {

			}
		},
		watch: {
			"path": {
				handler(newPath : string, oldPath : string) {
					this.loadUrl(newPath)
				},
				immediate: false //创建时是否通过此方法更新属性，默认值为false  
			},
			"loop": {
				handler(newLoop : boolean, oldLoop : boolean) {
					if (this.$el != null) {
						if (newLoop) {
							this.$el.loops = 0
						} else {
							// 不循环则设置成1次
							this.$el.loops = 1
						}
					}
				},
				immediate: false
			},
			"fillMode": {
				handler(newMode : string, oldMode : string) {
					if (newMode == "Backward" || newMode == "Forward" || newMode == "Clear") {
						if (this.$el != null) {
							this.$el.fillMode = newMode;
						}
					} else {
						// 非法入参，不管
					}
				},
				immediate: false
			},
			"hidden": {
				handler(newValue : boolean, oldValue : boolean) {
					if (newValue) {
						this.$el.isHidden = true
					} else {
						this.$el.isHidden = false
					}

				},
				immediate: false //创建时是否通过此方法更新属性，默认值为false  
			},
		},
		expose: ['loadUrl', 'startAnimation', 'pauseAnimation'],
		methods: {
			loadUrl(url : string) {
				if (this.loop) {
					this.$el.loops = 0
				} else {
					// 不循环则设置成1次
					this.$el.loops = 1
				}
				this.$el.clearsAfterStop = true;
				let svgaParser = new SVGAParser();
				var fileUrl : URL | null

				if (url.hasPrefix("http")) {
					fileUrl = new URL(string = url)
				} else {
					const filePath = UTSiOS.getResourcePath(url)
					fileUrl = new URL(fileURLWithPath = filePath)
				}
				svgaParser.parse(with = fileUrl!, completionBlock = (videoItem ?: SVGAVideoEntity) : void => {
					this.$el.videoItem = videoItem;
					this.$el.startAnimation();

					let ret : Map<string, any> = new Map();
					let detail : Map<string, any> = new Map();
					detail.set("detail", ret)
					this.__$$emit("start", detail);
				}, failureBlock = (error ?: NSError) : void => {
					let ret : Map<string, any> = new Map();
					let detail : Map<string, any> = new Map();
					detail.set("detail", ret)
					this.__$$emit("error", detail);
				});
			},
			startAnimation() {
				this.$el.startAnimation();
			},
			pauseAnimation() {
				this.$el.pauseAnimation();
			},
		},
		NVLoad() : SVGAPlayer {
			playerDelegate = new ISVGAPlayerDelegate(this);
			let player = new SVGAPlayer();
			player.delegate = playerDelegate;
			return player
		},
		NVLoaded() { //原生View已创建  
			this.loadUrl(this.path)
		},
	}

	class ISVGAPlayerDelegate implements SVGAPlayerDelegate {
		private comp : UTSComponent<SVGAPlayer>

		constructor(comp : UTSComponent<SVGAPlayer>) {
			this.comp = comp
			super.init()
		}

		svgaPlayerDidFinishedAnimation(_player : SVGAPlayer) : void {
			let ret : Map<string, any> = new Map();
			let detail : Map<string, any> = new Map();
			detail.set("detail", ret)
			this.comp.__$$emit("finished", detail);
		}

		svgaPlayer(_player : SVGAPlayer, @argumentLabel("didAnimatedToFrame") frame : Int) {
			let ret : Map<string, any> = new Map();
			ret.set("frame", frame);

			let detail : Map<string, any> = new Map();
			detail.set("detail", ret)
			this.comp.__$$emit("frame", detail);
		}

		svgaPlayer(_player : SVGAPlayer, @argumentLabel("didAnimatedToPercentage") percentage : Float) {
			let ret : Map<string, any> = new Map();
			ret.set("percentage", percentage);

			let detail : Map<string, any> = new Map();
			detail.set("detail", ret)
			this.comp.__$$emit("percentage", detail);
		}
	}
	let playerDelegate : ISVGAPlayerDelegate | null = null
</script>

<style>
</style>